package com.barber.controller;



import com.barber.entity.Customer;
import com.barber.service.CustomerService;

import com.barber.util.MD5;
import com.barber.util.SendEmail;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/customer")
public class CustomerController extends HttpServlet {
    Logger logger=Logger.getLogger(CustomerController.class);
//    CustomerService cusService=new CustomerServiceImpl();

    @Autowired
    CustomerService customerService;
    @GetMapping("/getinfo")//个人信息
    Map getinfo(HttpServletRequest request, HttpServletResponse response, @RequestParam Map map) throws IOException {
        logger.debug(map);
		Map	islogin= (Map) request.getSession().getAttribute("islogin");
		logger.debug(islogin);
	    map.putAll(islogin);
	    logger.debug(map);
        Map m=customerService.getinfo(map);
        logger.debug(m);
        if (m!=null) {
            return m;
        } else {
            return null;
        }
    }
    @PostMapping("/register")
    String sregister(HttpServletRequest request, HttpServletResponse response, Customer customer, @RequestParam Map map) throws IOException {
        logger.debug(customer);
        logger.debug(map);
        if (customerService.cregister(map)) {
            return "Ok";
        } else {
            return "FAIL";
        }
    }
    @GetMapping("/login")
    String login(HttpServletRequest request, @RequestParam Map map) throws IOException{

      Map m=customerService.login(map);

      logger.debug(m); //{password=lll, phone=111111, usertype=2, userid=3, username=哒玲玲}
      if(m!=null){
          request.getSession().setAttribute("islogin",m);
          m.put("usertype",2);
          return("success");
      }else {
          request.getSession().setAttribute("islogin", null);
          return ("用户名或密码错误");
      }
    }
    @GetMapping("/logout")//登录hash表返回两个数据
    protected void  logout(HttpServletRequest request,HttpServletResponse response, @RequestParam Map map) throws IOException {
        request.getSession().setAttribute("islogin",null);
        response.sendRedirect("bye");

    }
    @PostMapping()
    Map select(@RequestParam Map map, HttpServletRequest req) throws IOException {
        logger.debug(map);//{key=abc, pageIndex=0, pageSize=10, sortField=createtime, sortOrder=desc}
        Map b=customerService.getCustomers(map);

        logger.debug("select success");
        return b;
    }
    @PostMapping("/insert")
    String insert(HttpServletRequest request, HttpServletResponse response,@RequestBody  Customer customer)throws IOException
    {

//        String data = request.getParameter("customer");
//        System.out.println(data);
//        data = data.substring(1, data.length() - 1);
//        System.out.println(data);
//        Customer customer = JSON.parseObject(data, Customer.class);

        if (customerService.inserCustomer(customer))
            return("OK");
        else
            return("FAIL");
    }
    @GetMapping("/delete")//spring风格 前端传的是	url : "menu2/delete/"+menuid,//Spring风格
    String delete(HttpServletResponse response, HttpServletRequest request,@RequestParam Map map ) throws IOException {
        logger.debug(map);
        if (customerService.deleteCustomer(map)) {
            return ("success!");
        } else {
            return("delete failure!");

        }
    }

    @PostMapping("/update")
    String update(HttpServletRequest request, HttpServletResponse resp,@RequestBody  Customer customer) throws IOException {
//
        HashMap m=(HashMap) request.getSession().getAttribute("islogin");
        int id= (int) m.get("userid");
        customer.setUserid(id);
        logger.debug(customer);
        String pass= (String) m.get("PASSWORD");
        customer.setPassword(pass);
        if(customerService.updateCustomer(customer))
            return("OK");
        else
            return("FAIL");
    }
    @PostMapping("/changewd")
    String changewd(HttpServletRequest request, HttpServletResponse resp,@RequestBody  Customer customer) throws IOException {
//
        logger.debug(customer);
        if(customerService.updateCustomer(customer))
            return("OK");
        else
            return("FAIL");
    }
    private void logout(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        request.getSession().setAttribute("customer",null);
        resp.sendRedirect("bye");
    }
    @GetMapping("/forgetpwd")//登录hash表返回两个数据
    String forgetpwd(HttpServletRequest request, @RequestParam Map map) throws IOException {
//        logger.debug(map);
        String email= (String) map.get("email");
        logger.debug(email);
//        Map map1=customerService.password(map);
        String password= (String) map.get("password");


            SendEmail.sendEmail( email,  password);
            map.put("password",MD5.MD5Encode(password));//加密修改 原密码登录
            logger.debug(map);
        if (customerService.updatepass(map)) {
            return  "success";
        } else {
            return "FAIL";
        }
    }

    @GetMapping("/changewd")
    String changewd(HttpServletRequest request, HttpServletResponse response, @RequestParam Map map) throws IOException {
        logger.debug("11111111111");
        Map	islogin= (Map) request.getSession().getAttribute("islogin");
        logger.debug(islogin);
        map.putAll(islogin);
        logger.debug(map);


        String oldpassword= (String) map.get("oldpassword");
        logger.debug(oldpassword);


        String password= (String) map.get("password");
        if(password.equals(oldpassword)){//相同
            return ("olderror");
//            return;
        }
        map.put("password",MD5.MD5Encode(password));//加密修改 原密码登录
        Boolean c=customerService.updatepass(map);
        logger.debug(c);
        if (c) {

            return "success";
        } else {
            return null;
        }
    }

}
